<?php

Yii::import( "application.controllers.FrontendBaseController" );

class VideoController extends FrontendBaseController
{
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView( $id )
    {
        $model = $this->loadModel( $id );

        $isAuthor = VideoFacade::isAuthor( $model );
        if ( $isAuthor )
        {
            $user = $this->getUserFromSession();
            $nameText = "Моя страница";
            $videoText = "Мои видео";
        }
        else
        {
            $user = VideoFacade::getAuthorUser( $model );
            $nameText = $user->name . " " . $user->surname;
            $videoText = "Видео";
        }
        $this->breadcrumbs = array( "Главная" => "/",
            $nameText => $this->getUserMainPageUrl( $user->user_id ),
            $videoText => "/video/" . $user->user_id,
            $model->title => "",
        );
        $this->render( 'view', array(
            'model' => $model,
            'isAuthor' => $isAuthor,
            'author' => $user
        ) );
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $this->breadcrumbs = array( "Главная" => "/",
            "Моя страница" => $this->getUserMainPageUrl(),
            "Моё видео" => "/video/",
            "Добавление видео" => "",
        );
        $model = new Video;

        if ( isset( $_POST[ 'Video' ] ) )
        {
            $model->attributes = $_POST[ 'Video' ];
            if ( VideoFacade::saveModel( $model ) )
                $this->redirect( array( 'view', 'id' => $model->video_id ) );
        }

        $this->render( 'create', array(
            'model' => $model,
        ) );
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate( $id )
    {
        $model = $this->loadModel( $id );

        $this->breadcrumbs = array( "Главная" => "/",
            "Моя страница" => $this->getUserMainPageUrl(),
            "Моё видео" => "/video/",
            "Редактирование видео &laquo;" . $model->title . "&raquo;" => "",
        );

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if ( isset( $_POST[ 'Video' ] ) )
        {
            $model->attributes = $_POST[ 'Video' ];
            if ( VideoFacade::saveModel( $model ) )
                $this->redirect( array( 'view', 'id' => $model->video_id ) );
        }

        $this->render( 'update', array(
            'model' => $model,
        ) );
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete( $id )
    {
        // we only allow deletion via POST request
        VideoFacade::deleteModel($id);

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if ( !isset( $_GET[ 'ajax' ] ) )
            $this->redirect( isset( $_POST[ 'returnUrl' ] ) ? $_POST[ 'returnUrl' ] : array( 'index' ) );
    }

    /**
     * Lists all models.
     */
    public function actionIndex( $id = null )
    {
        if ( (!isset( $id )) || ($id == $this->getUserIdFromSession()) )
        {
            $isAuthor = true;
        }

        if ( !$isAuthor )
        {
            $user = UserFacade::getUserById( $id );
            $nameText = $user->name . " " . $user->surname;
            $videoText = "Видео";
        }
        else
        {
            $user = $this->getUserFromSession();
            $nameText = "Моя страница";
            $videoText = "Моё видео";
        }
        $this->breadcrumbs = array( "Главная" => "/",
            $nameText => $this->getUserMainPageUrl( $id ),
            $videoText => "" );

        
        $dataProvider = VideoFacade::getDataProviderByUser($user);
        $this->render( 'index', array(
            'dataProvider' => $dataProvider,
            'isAuthor' => $isAuthor
        ) );
    }

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model = new Video( 'search' );
        $model->unsetAttributes();  // clear any default values
        if ( isset( $_GET[ 'Video' ] ) )
            $model->attributes = $_GET[ 'Video' ];

        $this->render( 'admin', array(
            'model' => $model,
        ) );
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel( $id )
    {
        return VideoFacade::loadModel( $id );
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation( $model )
    {
        if ( isset( $_POST[ 'ajax' ] ) && $_POST[ 'ajax' ] === 'video-form' )
        {
            echo CActiveForm::validate( $model );
            Yii::app()->end();
        }
    }

    public function actionAll()
    {
        $model = new Video;
        $model->unsetAttributes();  // clear any default values
        $this->registerAjaxSearch("video-search", "video-list");
        $this->breadcrumbs = array( "Главная" => "/",
                                    "Всё видео" => "" );

        $title = $_GET['Video']['title'];
        $dataProvider = VideoFacade::getDataProviderForSearch($title);

        $this->render( 'all', array(
            'dataProvider' => $dataProvider,
            'searchModel' => $model
        ) );

    }

}
